package com.heima.common.exception;


import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局通用异常类增强
 */
@RestControllerAdvice
@Log4j2
public class ExceptionAdvice {

    /**
     * 业务类异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public ResponseResult doBusinessException(Exception e) {
        e.printStackTrace();
        //记录日志
        log.error("catch exception:{}", e.getMessage());
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "操作失败啦~~~请稍后再试！");
    }


    /**
     * 系统类异常
     */
    @ExceptionHandler(SystemException.class)
    public ResponseResult doSystemException(Exception e) {
        //实际将问题显示收到redis服务器。运行查询检查
        //记录日志
        e.printStackTrace();
        log.error("catch exception:{}", e.getMessage());
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "系统服务异常~~~请稍后再试！");
    }


    /**
     * 其他异常
     */
    @ExceptionHandler(Exception.class)
    public ResponseResult Exception(Exception e) {
        e.printStackTrace();
        log.error("catch exception:{}", e.getMessage());
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "也不知道为啥就出错咯！！！");
    }
}
